<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function getDateAgo(d, num) {
        d.setDate(d.getDate() - num);
        return d.getDate();
      }
      let d1 = new Date(2015, 0, 2);
      console.log(getDateAgo(d1, 1)); // 1, (1 Jan 2015)
      let d2 = new Date(2015, 0, 2);
      console.log(getDateAgo(d2, 2)); // 31, (31 Dec 2014)
      let d3 = new Date(2015, 0, 2);
      console.log(getDateAgo(d3, 365)); // 2, (2 Jan 2014)
    </script>
  </body>
</html>
